package src.main.java.org.ATM;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    private Scanner sc = new Scanner(System.in);
    private ArrayList<Account> accounts = new ArrayList<>();
    private Account loginAcc; //记录登录后的用户账户

    public void start(){
        while (true) {
            System.out.println("====欢迎进入到了ATM系统====");
            System.out.println("1.用户登录");
            System.out.println("2.用户开户");
            System.out.println("请选择");
            String command = sc.nextLine();
            switch (command){
                case "1":
                    //登录
                    login();
                    break;
                case "2":
                    //开户
                    createAccount();
                    break;
                default:
                    System.out.println("没有该操作");
            }
        }
    }
    private void createAccount(){
        Account account = new Account();
        System.out.println("===开户界面===");
        System.out.println("请输入账户名称：");
        String name = sc.next();
        account.setUserName(name);

        while (true) {
            System.out.println("请输入您的性别：");
            char gender = sc.next().charAt(0);
            if(gender=='男' || gender == '女'){
                account.setGender(gender);
                break;
            }else{
                System.out.println("您输入的性别有误，请输入男或者女");
            }
        }

        while (true) {
            System.out.println("请输入您的密码：");
            String password = sc.next();
            System.out.println("请您输入您的确认密码：");
            String okPassword = sc.next();
            if(okPassword.equals(password)){
                account.setPassWord(password);
                break;
            }else{
                System.out.println("您输入的两次密码不一致，请重新输入");
            }
        }

        System.out.println("请您输入您的取现额度：");
        double limit = sc.nextDouble();
        account.setLimit(limit);

//        生成卡号
        String cardId = createCardId();
        account.setCardId(cardId);

        accounts.add(account);
        System.out.println("恭喜您："+account.getUserName()+"您的卡号是："+account.getCardId());
    }

//    返回八位数字卡号，不能重复
    private String createCardId(){
        while (true) {
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++) {
                int data = r.nextInt(10);
                cardId += data;
            }
//        判断cardId是否与其他账户卡号重复
            Account accountByCardId = getAccountByCardId(cardId);
            if(accountByCardId == null){
                return cardId;
            }
        }
    }
//    根据卡号查询账户对象
    private Account getAccountByCardId(String cardId){
        for (int i = 0; i < accounts.size(); i++) {
            Account account = accounts.get(i);
            if(account.getCardId().equals(cardId)){
                return account;
            }
        }
        return null;
    }
//    用户登录操作
    private void login(){
        System.out.println("===系统登录===");
        if(accounts.size()==0){
            System.out.println("当前系统中无账户，请先开户");
            return;
        }
        while (true) {
            System.out.println("请您输入您的登录卡号：");
            String cardId = sc.next();
            Account accountByCardId = getAccountByCardId(cardId);
            if(accountByCardId == null){
                System.out.println("您输入的登录卡号不存在，请确认！");
            }else{
                while (true) {
                    System.out.println("请输入登录密码：");
                    String passWord = sc.next();
                    if(accountByCardId.getPassWord().equals(passWord)){
                        loginAcc = accountByCardId;
                        System.out.println("恭喜您，"+accountByCardId.getUserName()+"登录成功，您的卡号是："+accountByCardId.getCardId());
//                        展示登录后的操作界面
                        showUserCommand();
                        return;
                    }else{
                        System.out.println("您输入密码不正确，请确认。");
                    }
                }
            }
        }

    }
//    展示登录后的操作界面
    private void showUserCommand(){
        while (true) {
            System.out.println(loginAcc.getUserName()+"您可以选择如下功能进行账户处理：");
            System.out.println("1.查询账户");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.密码修改");
            System.out.println("6.退出");
            System.out.println("7.注销当前账户");
            int command = sc.nextInt();
            switch (command){
                case 1:
                    showloginAccount();
                    break;
                case 2:
                    depositMoney();
                    break;
                case 3:
                    drawMoney();
                    break;
                case 4:
                    transferAccount();
                    break;
                case 5:
                    modifyPassWord();
                    return;
                case 6:
                    System.out.println(loginAcc.getUserName()+"您退出系统成功");
                    return;
                case 7:
                    boolean b = cancelAccount();
                    if(b){
                        return;
                        //回到欢迎界面
                    }
                    break;
                default:
                    System.out.println("您当前选择的操作不存在");
            }
        }

    }

    private void modifyPassWord() {
        System.out.println("==密码修改==");
        while (true) {
            System.out.println("请输入原密码");
            String oldPassWord = sc.next();
            if(loginAcc.getPassWord().equals(oldPassWord)){
                String newPassWord = null;
                while (true) {
                    System.out.println("请输入新密码：");
                    newPassWord = sc.next();
                    System.out.println("请再输入一次");
                    String newPassWord2 = sc.next();
                    if(newPassWord.equals(newPassWord2)){
                        loginAcc.setPassWord(newPassWord);
                        System.out.println("密码修改成功");
                        return;
                    }else {
                        System.out.println("两次输入的密码不一致，请重新输入");
                    }
                }
            }else{
                System.out.println("您输入的密码不正确，请重新输入！");
            }
        }
    }

    private boolean cancelAccount() {
        System.out.println("==销户==");
        System.out.println("请问您确认销户吗?y/n");
        String command = sc.next();
        switch (command){
            case "y":
                if(loginAcc.getMoney()==0){
                    accounts.remove(loginAcc);
                    System.out.println("您的账号已成功销户");
                    return true;
                }else{
                    System.out.println("对不起您的账户中还有钱，不允许销户");
                    return false;
                }
            default:
                System.out.println("好的，您的账户保留！！");
                return false;
        }
    }

    private void transferAccount() {
        System.out.println("==转账界面==");
        if(accounts.size()<2){
            System.out.println("当前系统中只有一个账户");
            return;
        }
        if(loginAcc.getMoney()==0){
            System.out.println("您账户余额为0");
            return;
        }
//        真正转账
        while (true) {
            System.out.println("请您输入对方卡号：");
            String cardId = sc.next();
            Account accountByCardId = getAccountByCardId(cardId);
            if(accountByCardId == null){
                System.out.println("您输入的对方卡号不存在");
            }else{
                String userName = "*"+accountByCardId.getUserName().substring(1);
                System.out.println("请您输入【"+userName+"】姓氏");
                String preName = sc.next();
                if(accountByCardId.getUserName().startsWith(preName)){
                    while (true) {
                        System.out.println("请你输入转账金额");
                        double money = sc.nextDouble();
                        if(loginAcc.getMoney()>=money){
                            loginAcc.setMoney(loginAcc.getMoney()-money);
                            accountByCardId.setMoney(accountByCardId.getMoney()+money);
                            System.out.println("您成功转账了"+money+"当前余额为+"+loginAcc.getMoney());
                            return; //跳出转账方法
                        }else{
                            System.out.println("您的余额不足，最多可转"+loginAcc.getMoney());
                        }
                    }
                }else {
                    System.out.println("对不起，您输入的姓氏有问题。");
                }
            }
        }

    }

    /*取钱*/
    private void drawMoney() {
        System.out.println("==取钱==");
        if(loginAcc.getMoney()<100){
            System.out.println("账户余额不足100元");
            return;
        }
        while (true) {
            System.out.println("请您输入取款金额：");
            double money = sc.nextDouble();

            if(loginAcc.getMoney()>=money){
                if(money>loginAcc.getLimit()){
                    System.out.println("您当前取款金额超过限额，您每次最多可取："+loginAcc.getLimit());
                }else {
                    loginAcc.setMoney(loginAcc.getMoney()-money);
                    System.out.println("您取款"+money+"成功，当前余额"+loginAcc.getMoney());
                    break;
                }
            }else{
                System.out.println("余额不足，您的账户中的余额是："+loginAcc.getMoney());
            }
        }
    }

    private void depositMoney() {
        System.out.println("==存钱操作==");
        System.out.println("请您输入存款金额");
        double money = sc.nextDouble();

        loginAcc.setMoney(loginAcc.getMoney()+money);
        System.out.println("恭喜您存钱："+money+"成功,当前余额为："+loginAcc.getMoney());
    }

    private void showloginAccount(){
        System.out.println("当前账户信息是：");
        System.out.println("卡号是："+loginAcc.getCardId());
        System.out.println("户主是："+loginAcc.getUserName());
        System.out.println("性别是："+loginAcc.getGender());
        System.out.println("余额是："+loginAcc.getMoney());
        System.out.println("每次取现额度是："+loginAcc.getLimit());

    }

}
